//
//  ST_TrophyChildDetailViewController.swift
//  btvideo
//
//  Created by mac on 2021/7/7.
//  Copyright © 2021 JarvanZhang. All rights reserved.
//

import UIKit
import MJRefresh
import JXSegmentedView
import SwifterSwift


enum ST_TrophyRankType:String,CaseIterable{
    case 魅力 = "vdsns/EarningRank"
    case 财富 = "vdsns/rechargeRank"
    case 亲密度 = "vdsns/intimacyRank"
    
    var widgetColor:UIColor{
        switch self {
        case .亲密度:
           return  #colorLiteral(red: 0.9960784314, green: 0.4509803922, blue: 0.4431372549, alpha: 1)
        case .财富:
            return #colorLiteral(red: 0.8156862745, green: 0.6470588235, blue: 0.4235294118, alpha: 1)
        case .魅力:
            return #colorLiteral(red: 0.5882352941, green: 0.4980392157, blue: 1, alpha: 1)
        }
    }
    
    var gradient_model:ST_FWGradientViewModel{
        switch self {
        case .亲密度:
           return  ST_FWGradientViewModel(colors: [#colorLiteral(red: 0.9960784314, green: 0.4235294118, blue: 0.4235294118, alpha: 1),#colorLiteral(red: 0.9960784314, green: 0.6862745098, blue: 0.6, alpha: 1)], startPoint: CGPoint(x: 0, y: 0), endPoint: CGPoint(x: 0, y: 1))
        case .财富:
            return ST_FWGradientViewModel(colors: [#colorLiteral(red: 0.8438429236, green: 0.6900256872, blue: 0.4820910692, alpha: 1),#colorLiteral(red: 0.9529411765, green: 0.8078431373, blue: 0.5882352941, alpha: 1)], startPoint: CGPoint(x: 0, y: 0), endPoint: CGPoint(x: 0, y: 1))
        case .魅力:
            return ST_FWGradientViewModel(colors: [#colorLiteral(red: 0.5882352941, green: 0.4980392157, blue: 1, alpha: 1),#colorLiteral(red: 0.7882352941, green: 0.6470588235, blue: 1, alpha: 1)], startPoint: CGPoint(x: 0, y: 0), endPoint: CGPoint(x: 0, y: 1))
        }
    }
    
}

enum ST_TrophyRankTimeintervalStyle:String,CaseIterable {
    case 日 = "/day/v2.json"
    case 周 = "/week/v2.json"
    case 月 = "/month/v2.json"
}


class ST_TrophyChildDetailViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .clear
        
         
        
        view.addSubview(collectionView)
        
        collectionView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        view.addSubview(bottom_view)
        bottom_view.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(78+BTSafeAreaBottom)
        }
        
//        collectionView.backgroundColor = .random
        
//        collectionView.mj_header = MJRefreshNormalHeader.init(refreshingBlock: {[weak self] in
//            self?.request_data()
//        })
        
        request_data()
    }
    
//    fileprivate var currentPage = 0

    fileprivate func request_data(){
        
        JVrequest(api) { (json) in
            
            
            if [ST_TrophyRankType.魅力,ST_TrophyRankType.财富].contains(self.rankType) && json["data"]["in_rank"].boolValue == false {
                
                self.collectionView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: BTSafeAreaBottom+80, right: 0)
                
                self.bottom_view.alpha = 1
                
                self.bottom_view.bind(diff: json["data"]["diff_currency"].floatValue, current: json["data"]["currency"].floatValue, rankType: self.rankType)
                
            }
            
            var list:[Any]
            
            switch self.rankType{
            case .亲密度:
                list = json["data"]["list"].arrayValue.map({ ST_TrophyIntimacyCellData(fromJson: $0) })
            default:
                list = json["data"]["list"].arrayValue.map({ ST_TrophyNormalCellData(fromJson: $0, rankType: self.rankType) })
            }
            
            let res = self.group(array: list)
            
            self.header_array = res.headerData
            
            self.data_array = res.cellData
            
            self.collectionView.reloadData()
            
            self.collectionView.mj_header?.endRefreshing()
            
        } failure: { (code, err) in
            
            self.toast(text: err)
            self.collectionView.mj_header?.endRefreshing()
        }
    }
    
    
    fileprivate func group<T: Any>(array:[T]) -> (headerData:[T],cellData:[T]) {
        
        let k = array.count - 3
        
        return (headerData:Array(array.prefix(3)),cellData:Array(array.suffix(k < 0 ? 0 : k)))
        
    }

    
    var rankType = ST_TrophyRankType.魅力
    
    var timeinterval_type = ST_TrophyRankTimeintervalStyle.日
    
    
    fileprivate lazy var api = PH.BASE_URL + rankType.rawValue + timeinterval_type.rawValue
//    MARK: - Properties
    
    fileprivate lazy var layout:UICollectionViewFlowLayout = {
        
        let controlLayout = UICollectionViewFlowLayout()
        
        controlLayout.headerReferenceSize = CGSize(width: ScreenWidth, height: 182)
        
        controlLayout.minimumLineSpacing = 0
        
        controlLayout.minimumInteritemSpacing = 0
        
        controlLayout.sectionInset = UIEdgeInsets.init(top: 0, left: 0, bottom: BTSafeAreaBottom, right: 0)
        
        controlLayout.itemSize = CGSize.init(width: ScreenWidth, height: 78)
        
        return controlLayout
    }()
    
    fileprivate lazy var collectionView:UICollectionView = {
        let control = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: self.layout)
        control.delegate = self
        control.dataSource = self
        
        control.register(cellWithClass: ST_TrophyIntimacyCell.self)
        control.register(cellWithClass: ST_TrophyNormalCell.self)
        
        control.register(supplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withClass: ST_TropNormalHeaderView.self)
        
        control.register(supplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withClass: ST_TropyIntimacyHeaderView.self)
        
        
        if #available(iOS 11.0, *) {
            control.contentInsetAdjustmentBehavior = .never
        }
        control.contentInset = UIEdgeInsets.zero
//        control.alwaysBounceVertical = true
        control.showsVerticalScrollIndicator = false
        control.showsHorizontalScrollIndicator = false
        control.backgroundColor = .clear
        return control
    }()
    
    fileprivate lazy var data_array = [Any]()
    
    fileprivate lazy var header_array = [Any]()
    
    
    fileprivate lazy var bottom_view:ST_TrophyRankDistanceView = {
       let control = ST_TrophyRankDistanceView()
        control.alpha = 0
        return control
    }()
    
}


extension ST_TrophyChildDetailViewController:UICollectionViewDataSource,UICollectionViewDelegate{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return data_array.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        var cell:UICollectionViewCell
            
        switch rankType{
        case .亲密度:
           
            let iCell = collectionView.dequeueReusableCell(withClass: ST_TrophyIntimacyCell.self, for: indexPath)
            
            if let data = data_array[safe: indexPath.row] as? ST_TrophyIntimacyCellData{
                iCell.bind(data: data, index: indexPath.row)
            }
            
            cell = iCell
        case .财富,.魅力:
            
            let iCell = collectionView.dequeueReusableCell(withClass: ST_TrophyNormalCell.self, for: indexPath)
            
            if let data = data_array[safe: indexPath.row] as? ST_TrophyNormalCellData{
                iCell.bind(data: data, index: indexPath.row)
            }
            
             cell = iCell
        }
        
        return cell
    }
    
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        
        
        switch rankType{
        case .亲密度:
            
            let view =  collectionView.dequeueReusableSupplementaryView(ofKind: kind, withClass: ST_TropyIntimacyHeaderView.self, for: indexPath)
            
            if let headerList = self.header_array as? [ST_TrophyIntimacyCellData]{
                view.bind(data_array: headerList)
            }
            return view
         
        case .财富,.魅力:
            
            let view =  collectionView.dequeueReusableSupplementaryView(ofKind: kind, withClass: ST_TropNormalHeaderView.self, for: indexPath)
            
            if let headerList = self.header_array as? [ST_TrophyNormalCellData]{
                view.bind(data_array: headerList)
            }
            return view
        }
        
        
        
    }
    
}

extension ST_TrophyChildDetailViewController: JXSegmentedListContainerViewListDelegate {
    
    func listView() -> UIView {
        return view
    }
}
